package se.webbzon.boltzmann.game.sequence.builder.dialogue;

import java.util.ArrayList;
import java.util.List;

import se.webbzon.boltzmann.game.object.dialogue.ChoiceDialogue;
import se.webbzon.boltzmann.game.object.dialogue.DialogueBackground;
import se.webbzon.boltzmann.game.object.dialogue.layout.ChoiceDialogueLayout;
import se.webbzon.boltzmann.game.sequence.ChoiceDialogueSequenceNode;
import se.webbzon.boltzmann.game.sequence.Sequence;
import se.webbzon.boltzmann.game.sequence.SequenceNode;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilder;
import se.webbzon.boltzmann.game.sequence.builder.SequenceRowDeflation;
import se.webbzon.oschi01.worldarea.WorldArea;

public class ChoiceDialogueRow extends AbstractDialogueRow {
	
	// The name of the sequence row
	public static final String name = "choiceDialogue";
	
	// A list of all the choices of this choice dialogue
	private final List<String> choices;
	
	/*============================================================
	Constructors
	============================================================*/

	public ChoiceDialogueRow(	SequenceBuilder builder,
								ChoiceDialogueLayout layout,
								DialogueBackground background,
								String text) {
		super(name,builder,layout,background,text);
		choices = new ArrayList<String>();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Adds a choice to the choice dialogue row. **/
	public boolean addChoice(String choice) {
		return choices.add(choice);
	}
	
	/** Removes a choice from the choice dialogue row. **/
	public boolean removeChoice(String choice) {
		return choices.remove(choice);
	}
	
	/** Clears all the choices from the choice dialogue row. **/
	public void clearChoices() {
		choices.clear();
	}
	
	/** Returns the number of available choices in this choice dialogue row.**/
	public int choices() {
		return choices.size();
	}
	
	/** Returns the (n+1):th choice in this choice dialogue row. **/
	public String getChoice(int n) {
		return choices.get(n);
	}
	
	@Override public int maxLinks() {
		return choices.size();
	}

	@Override public int minLinks() {
		return 0;
	}

	@Override public SequenceNode compile(WorldArea world, Sequence sequence) {
		ChoiceDialogueLayout layout = (ChoiceDialogueLayout) getLayout();
		ChoiceDialogue dialogue = 
				new ChoiceDialogue(layout,getBackground(),getText(),choices);
		world.createInstance(dialogue);
		return new ChoiceDialogueSequenceNode(sequence,dialogue);
	}

	@Override public void bind(SequenceNode node, SequenceNode[] nodes) {
		ChoiceDialogueSequenceNode n = (ChoiceDialogueSequenceNode) node;
		SequenceNode[] branches = new SequenceNode[links()];
		for (int i = 0; i < links(); i++) {
			Integer branch = getLink(i);
			if (branch != null)
				branches[i] = nodes[branch];
		}
		n.setBranches(branches);
	}

	@Override public SequenceRowDeflation getDeflation() {
		return new ChoiceDialogueRowDeflation(this);
	}

}
